package LuceneIndexCreate;

import com.alibaba.fastjson.JSON;
import LuceneIndexCreate.http.Host;
import LuceneIndexCreate.model.CatIdsFrom1688;
import LuceneIndexCreate.model.CatIndexData;
import org.apache.http.HttpHost;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;

/**
 * Created by User on 2017/12/6.
 */
public class GetDesc {

    public static void main(String[] args) throws IOException, InterruptedException {
        String json = "{\"numiids\":[\"556518781605\",\"558687117609\",\"540370421333\",\"555755483389\",\"561966634589\",\"550556660742\",\"535521738317\",\"558054251553\",\"555133214072\",\"521017936502\",\"560492785439\",\"520638976997\",\"521518151459\",\"562070818871\",\"547758058099\",\"547678814898\",\"544154684354\",\"558803874393\",\"542170637860\",\"541865161109\"],\"keyword\":\"钱包\",\"cid\":\"121434005\"}";
        CatIdsFrom1688 catIdsFrom1688 = JSON.parseObject(json, CatIdsFrom1688.class);

        CatIndexData data = getDate(catIdsFrom1688);

        System.out.println(JSON.toJSONString(data));

    }

    public static CatIndexData getDate(CatIdsFrom1688 catIdsFrom1688) throws IOException, InterruptedException {
        String cid = catIdsFrom1688.getCid();
        String keyword = catIdsFrom1688.getKeyword();
        List<String> numiids = catIdsFrom1688.getNumiids();

        StringBuilder stringBuilder = new StringBuilder();
        for (String itemId : numiids) {
            String desc = getDesc(itemId, 3);
            if (desc != null && !"".equals(desc.trim())) {
                stringBuilder.append(desc).append(",");
            }
        }

        return new CatIndexData(cid, keyword, stringBuilder.toString());
    }

    public static String getDesc(String itemId, int retry) throws IOException, InterruptedException {
        if (retry < 0) return "";
        if (StringUtils.isEmpty(itemId)) return "";
        String itemUrl = "https://detail.1688.com/offer/" + itemId + ".html";
        String desc = null;
        try {
            HttpHost host = Host.getHost();
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host.getHostName(), host.getPort()));
            org.jsoup.nodes.Document document = Jsoup.connect(itemUrl).referrer("https://m.1688.com/").get();
            Elements title = document.getElementsByAttributeValue("name", "keywords");
            desc = title.attr("content");
        } catch (Exception e) {
            System.out.println("retry " + itemId);
            Thread.sleep(100);
            return getDesc(itemId, --retry);
        }

        if (desc == null || "".equals(desc)) {
            System.out.println("retry " + itemId);
            Thread.sleep(1000);
            return getDesc(itemId, --retry);
        }

        return desc;
    }
}
